package com.yanqu.road.server.manger.cricket.skillhandler.startround.handler;

import com.yanqu.road.entity.cricket.helper.AddHpResult;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.gameplayer.module.cricket.battle.CricketGame;
import com.yanqu.road.server.gameplayer.module.cricket.battle.eCricketBattleRoundStep;
import com.yanqu.road.server.gameplayer.module.cricket.model.BattleData;
import com.yanqu.road.server.gameplayer.module.cricket.model.SkillData;
import com.yanqu.road.server.manger.cricket.skillhandler.startround.CricketSkillStartRoundStrategy;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class CricketSkill155Strategy implements CricketSkillStartRoundStrategy {

    /**
     * 技能处理-万剑归宗
     * @param cricketGame 本局对局对象
     * @param skillData 技能数据
     * @param action 我方
     * @param target 对方
     * @param roundId 第几回合
     * @param attackRoundBuilder 返回PB结构体
     * @return
     */
    @Override
    public boolean skillHandle(CricketGame cricketGame, SkillData skillData, BattleData action, BattleData target, int roundId, CricketProto.CricketAttackRound.Builder attackRoundBuilder) {
        if (skillData.getOverlay() >= skillData.getSkillEffect().getP2()) {
            skillData.effectAmountInc();
            skillData.setOverlay(skillData.getOverlay() - skillData.getSkillEffect().getP2());
            CricketProto.CricketBattleFrame.Builder newBattleFrameBuilder = cricketGame.newBattleFrameBuilder();
            newBattleFrameBuilder.setActionUserId(action.getUserId());
            newBattleFrameBuilder.setTargetUserId(target.getUserId());
            newBattleFrameBuilder.setSkillId(skillData.getSkillId());
            long attack = BigDecimal.valueOf(action.getPower()).multiply(BigDecimal.valueOf(skillData.getSkillEffect().getP1())).divide(BigDecimal.valueOf(1000), 8, RoundingMode.DOWN).longValue();
            AddHpResult addHpResult1 = target.addHp(-attack);
            newBattleFrameBuilder.setAttack(addHpResult1.getValue());
            newBattleFrameBuilder.setIsKill(addHpResult1.isKill());
            newBattleFrameBuilder.setHarmEffectSkillId(addHpResult1.getReduceHpEffectSkillId());
            newBattleFrameBuilder.setIsMiss(addHpResult1.isMissFlag());
            cricketGame.fillRemainHp(newBattleFrameBuilder);
            attackRoundBuilder.addStartRound(newBattleFrameBuilder);
            if (cricketGame.isOver(attackRoundBuilder, eCricketBattleRoundStep.StartRound.getValue(), newBattleFrameBuilder.getBattleFrameId())) {
                return true;
            }
        }
        return false;
    }
}
